package com.china08.yxyapi.api.manage.stu.mystudy.mistakes;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.mistakes.StuMistakesQuesNote;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.mistakes.StuMistakesQuesNoteRepository;

/**
 * 错题本--学生端--编辑/保存
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class YxyMistakesQuesNoteCrtController extends YxyApiBaseController {

    @Autowired
    private StuMistakesQuesNoteRepository stuMistakesQuesNoteRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/mistakes/note/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel, RespModel respModel) {
        String studentId = reqModel.studentId;
        String studentNick = getProfile(request, response).getUserNick();
        String quesId = reqModel.quesId;
        String note = reqModel.note;

        StuMistakesQuesNote stuMistakesQuesNotes;
        // 如果没有笔记进行保存
        if (reqModel.id == null || reqModel.id.equals("")) {
            stuMistakesQuesNotes = new StuMistakesQuesNote();
            stuMistakesQuesNotes.setStudentId(studentId);
            stuMistakesQuesNotes.setStudentNick(studentNick);
            stuMistakesQuesNotes.setQuesId(quesId);
            stuMistakesQuesNotes.setNote(note);
            stuMistakesQuesNotes.setPublishDate(new Date());
        }
        // 如果有笔记，进行编辑
        else {
            stuMistakesQuesNotes = stuMistakesQuesNoteRepository.findOne(reqModel.id);
            stuMistakesQuesNotes.setNote(note);
        }
        stuMistakesQuesNoteRepository.save(stuMistakesQuesNotes);

    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {
        // 学生ID
        public String studentId;
        // 笔记ID *修改时必传
        public String id;
        // 试题ID *修改时不用传
        public String quesId;
        // 笔记内容
        public String note;
    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {
    }
}
